package com.sc.nft.admin.controller;

import com.google.common.collect.Lists;
import com.sc.nft.admin.request.AddMenuRequest;
import com.sc.nft.admin.request.DistributionMenuRequest;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.UpdateMenuRequest;
import com.sc.nft.admin.vo.MenuResultVO;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.Menu;
import com.sc.nft.entity.RoleMenuRelation;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.MenuService;
import com.sc.nft.service.RoleMenuRelationService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单表服务控制器
 *
 * @author zhangxiangyu
 * @since 2021-04-03 15:15:03
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/menu")
@Api(tags="后台菜单模块")
public class MenuController   {
    private final MenuService menuService;
    private final RoleMenuRelationService roleMenuRelationService;


    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加菜单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin(adminAuthority=true)
    public Result add(@RequestBody AddMenuRequest requestMenu) throws GlobalException {
        Menu menu = new Menu();
        BeanUtils.copyProperties(requestMenu,menu);
        if(menu.getParentId()==null){
            menu.setParentId(0L);
        }
        return Result.success(menu.insert());
    }
    @GetMapping(value="/query", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询菜单", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin
    public Result<List<MenuResultVO>> query() throws GlobalException {
        Long roleId = SysMemberPool.getMember().getRoleId();

//        List<MenuResultVO> menuResultVOS = MenuConstant.menuMap.get(roleId);
//        if(CollUtil.isEmpty(menuResultVOS)) {
        List<Menu> menuList = menuService.getMenu(roleId);
        List<MenuResultVO> menuResultVOList = Lists.newArrayList();

        //确定一级菜单
        menuList.stream().filter(menu -> menu.getParentId() == 0).forEach(menu -> {
            MenuResultVO menuResultVO = new MenuResultVO();
            BeanUtils.copyProperties(menu, menuResultVO);
            menuResultVO.setType("catalog");
            menuResultVOList.add(menuResultVO);
        });
        //确定二级菜单
        menuResultVOList.stream().forEach(menuResultVO -> {
            List<MenuResultVO> downMenus = Lists.newArrayList();
            menuList.stream().filter(menu -> menu.getParentId() != 0).forEach(menu -> {
                if (menu.getParentId().intValue() == menuResultVO.getId().intValue()) {
                    MenuResultVO secondMenu = new MenuResultVO();
                    List<MenuResultVO> downMenus1 = Lists.newArrayList();
                    for (Menu menu1 : menuList) {
                        if (menu1.getParentId()==menu.getId()){
                            MenuResultVO thirdMenu = new MenuResultVO();
                            BeanUtils.copyProperties(menu1, thirdMenu);
                            thirdMenu.setType("menu");
                            downMenus1.add(thirdMenu);
                        }
                    }
                    secondMenu.setDownMenu(downMenus1);
                    BeanUtils.copyProperties(menu, secondMenu);
                    secondMenu.setType("menu");
                    downMenus.add(secondMenu);
                }
            });
            menuResultVO.setDownMenu(downMenus);
        });
//            menuResultVOS = menuResultVOList;
//            MenuConstant.menuMap.put(roleId, menuResultVOS);
//        }
        return Result.success(menuResultVOList);
    }

    @GetMapping(value="/queryByRoleId", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询角色菜单", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin
    public Result<List<MenuResultVO>> queryByRoleId(Long roleId) throws GlobalException {

        List<Menu> menuList = menuService.getMenu(roleId);
        List<MenuResultVO> menuResultVOList = Lists.newArrayList();

        //确定一级菜单
        menuList.stream().filter(menu -> menu.getParentId() == 0).forEach(menu -> {
            MenuResultVO menuResultVO = new MenuResultVO();
            BeanUtils.copyProperties(menu, menuResultVO);
            menuResultVO.setType("catalog");
            menuResultVOList.add(menuResultVO);
        });
        //确定二级菜单
        menuResultVOList.stream().forEach(menuResultVO -> {
            List<MenuResultVO> downMenus = Lists.newArrayList();
            menuList.stream().filter(menu -> menu.getParentId() != 0).forEach(menu -> {
                if (menu.getParentId().intValue() == menuResultVO.getId().intValue()) {
                    MenuResultVO secondMenu = new MenuResultVO();
                    List<MenuResultVO> downMenus1 = Lists.newArrayList();
                    for (Menu menu1 : menuList) {
                        if (menu1.getParentId()==menu.getId()){
                            MenuResultVO thirdMenu = new MenuResultVO();
                            BeanUtils.copyProperties(menu1, thirdMenu);
                            thirdMenu.setType("menu");
                            downMenus1.add(thirdMenu);
                        }
                    }
                    secondMenu.setDownMenu(downMenus1);
                    BeanUtils.copyProperties(menu, secondMenu);
                    secondMenu.setType("menu");
                    downMenus.add(secondMenu);
                }
            });
            menuResultVO.setDownMenu(downMenus.stream().sorted(Comparator.comparing(MenuResultVO::getId)).collect(Collectors.toList()));
        });
//            menuResultVOS = menuResultVOList;
//            MenuConstant.menuMap.put(roleId, menuResultVOS);
//        }
        return Result.success(menuResultVOList);
    }




    @PutMapping(value="/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改菜单", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin(adminAuthority=true)
    public Result update(@RequestBody UpdateMenuRequest updateMenu) throws GlobalException {
        Menu menu = new Menu();
        BeanUtils.copyProperties(updateMenu,menu);
        return Result.success(menu.updateById());
    }

    @DeleteMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除菜单", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin(adminAuthority=true)
    public Result delete(@Valid @RequestBody IdRequest deleteMenu) throws GlobalException {
        new Menu().deleteById(deleteMenu.getId());
        return Result.success( );
    }


    @PostMapping(value="/distributionMenu", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分配菜单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @SysMemberLogin
    public Result distributionMenu(@RequestBody DistributionMenuRequest distributionMenu) throws GlobalException {
        List<RoleMenuRelation> roleMenuRelations = Lists.newArrayList();
        distributionMenu.getMenuIds().forEach(menuIds->{
            RoleMenuRelation roleMenuRelation = new RoleMenuRelation();
            roleMenuRelation.setMenuId(menuIds);
            roleMenuRelation.setRoleId(distributionMenu.getRoleId());
            roleMenuRelations.add(roleMenuRelation);
        });
        return Result.success(roleMenuRelationService.distributionMenu(roleMenuRelations,distributionMenu.getRoleId()));
    }


}